package cn.jiedh.app.meiweidk.login.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.HashMap;
import java.util.Map;

import cn.jiedh.app.meiweidk.R;
import cn.jiedh.app.meiweidk.Utils.tool.DensityUtil;

public class MyCustomView extends View {


    private static final String TAG = "MyCustomView";

    private float mCenterX;//绘制圆圈的中心坐标X;
    private float mCenterY;//绘制圆圈的中心坐标Y;

    private float mScaleRadius;//绘制刻度的半径
    private float mScaleLength = DensityUtil.dip2px(getContext(), 14);//刻度长度
    private float mScaleWidth = DensityUtil.dip2px(getContext(), 3f);//刻度宽度


    private float mEdgeRadius;//绘制外缘的半径
    private float mEdgeLength = DensityUtil.dip2px(getContext(), 3f);//外缘长度
    private float mEdgeWidth = DensityUtil.dip2px(getContext(), 14);//外缘宽度

    private float startX;//起始点X坐标
    private float startY;//起始点Y坐标
    private float stopX;//结束点X坐标
    private float stopY;//结束点Y坐标

    private Paint mPaintScale;//绘制刻度的画笔;
    private Paint mPaintEdge;//外缘画笔

    public MyCustomView(Context context) {
        this(context, null);
    }

    public MyCustomView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyCustomView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs, defStyleAttr);

    }


    private void init(AttributeSet attrs, int defStyle) {

//        Log.d(TAG, "init: ......");

        if (map == null) map = new HashMap<>();

        //设置绘制刻度画笔颜色：
        mPaintScale = new Paint(Paint.ANTI_ALIAS_FLAG);//给Paint加上抗锯齿标志。然后将Paint对象作为参数传给canvas的绘制方法。
        mPaintScale.setAntiAlias(true);
        mPaintScale.setStrokeWidth(mScaleWidth);
        mPaintScale.setStyle(Paint.Style.STROKE);
        mPaintScale.setColor(getResources().getColor(R.color.colorPrimary));

        //设置绘制外缘画笔颜色：
        mPaintEdge = new Paint(Paint.ANTI_ALIAS_FLAG);//给Paint加上抗锯齿标志。然后将Paint对象作为参数传给canvas的绘制方法。
        mPaintEdge.setAntiAlias(true);
        mPaintEdge.setStrokeWidth(mEdgeWidth);
//         mPaintEdge.setShader(R.drawable.edge_gradient);//设置渐变


        mPaintEdge.setStyle(Paint.Style.STROKE);
        mPaintEdge.setColor(getResources().getColor(R.color.colorPrimary));
//        mPaintEdge.setAlpha(150);


    }

    @Override
    protected void onDraw(Canvas canvas) {

//        Log.d(TAG, "onDraw: ..........");

        drawScale(canvas);

        if (whenScaleNum < maxScaleNum) whenScaleNum++;

    }


    //绘制最大刻度个数
    int maxScaleNum = 45;
    //当次绘制多少个刻度
    public int whenScaleNum = 0;

    int baseAlpha = 32;//基础透明度
    float dis = DensityUtil.dip2px(getContext(), 3);//内刻度与外刻度距离

    //画线
    public void drawScale(Canvas canvas) {


        int tempNum = whenScaleNum;

        if (whenScaleNum <= maxScaleNum) {

            for (int i = 0; i < tempNum; i++) {
                int alp = baseAlpha + (i + 1) * 5;
                if (alp >= 255) alp = 255;

                mPaintScale.setAlpha(alp);

                canvas.drawLine(map.get("startX" + i), map.get("startY" + i), map.get("stopX" + i), map.get("stopY" + i), mPaintScale);

            }


//            if (whenScaleNum == maxScaleNum) mPaintEdge.setAlpha(0);
            outeroordinate(whenScaleNum * 4);


//            Shader shader = new LinearGradient(outStopX, outStopY-outStopX, outStartX, outStartY, Color.parseColor("#0021FF"), Color.parseColor("#78FEFF"), Shader.TileMode.REPEAT);
//            Shader shader = new LinearGradient(outStartX, outStartY-outStartX, outStopX, outStopY, Color.RED, Color.YELLOW, Shader.TileMode.REPEAT);
            // 设置shader
//            mPaintEdge.setShader(shader);
            canvas.drawLine(outStartX, outStartY, outStopX, outStopY, mPaintEdge);
        }
    }


    /**
     * 计算 内刻度起始 ， 结束点的坐标
     */
    private void coordinate(int angle) {

        //这是第一条线的坐标

        startX = (float) Math.abs(mCenterX - mScaleRadius * Math.cos(angleVarietyradian(angle)));

        startY = (float) Math.abs(mCenterY - mScaleRadius * Math.sin(angleVarietyradian(angle)));

        stopX = (float) Math.abs(mCenterX - (mScaleRadius + mScaleLength) * Math.cos(angleVarietyradian(angle)));

        stopY = (float) Math.abs(mCenterY - (mScaleRadius + mScaleLength) * Math.sin(angleVarietyradian(angle)));

    }

    float outStartX = 0;
    float outStartY = 0;
    float outStopX = 0;
    float outStopY = 0;

    /**
     * 计算 外刻度起始 ， 结束点的坐标
     */
    private void outeroordinate(int angle) {

        //这是第一条线的坐标

//        outStartX = (float) Math.abs(mCenterX - mEdgeRadius * Math.cos(angleVarietyradian(angle)) - mEdgeWidth / 2 * Math.sin(angleVarietyradian(angle)));
//
//        outStartY = (float) Math.abs(mCenterY - mEdgeRadius * Math.sin(angleVarietyradian(angle)) + mEdgeWidth / 2 * Math.cos(angleVarietyradian(angle)));
//
//        outStopX = (float) Math.abs(mCenterX - (mEdgeRadius + mEdgeLength) * Math.cos(angleVarietyradian(angle)) - mEdgeWidth / 2 * Math.sin(angleVarietyradian(angle)));
//
//        outStopY = (float) Math.abs(mCenterY - (mEdgeRadius + mEdgeLength) * Math.sin(angleVarietyradian(angle)) + mEdgeWidth / 2 * Math.cos(angleVarietyradian(angle)));


        outStartX = (float) Math.abs(mCenterX - mEdgeRadius * Math.cos(angleVarietyradian(angle)) - mEdgeWidth / 2 * Math.sin(angleVarietyradian(angle)) - (mEdgeWidth / 2 + mScaleWidth / 2) * Math.sin(angleVarietyradian(angle)));

        outStopX = (float) Math.abs(mCenterX - (mEdgeRadius + mEdgeLength) * Math.cos(angleVarietyradian(angle)) - mEdgeWidth / 2 * Math.sin(angleVarietyradian(angle)) - (mEdgeWidth / 2 + mScaleWidth / 2) * Math.sin(angleVarietyradian(angle)));

        outStartY = (float) Math.abs(mCenterY - mEdgeRadius * Math.sin(angleVarietyradian(angle)) + mEdgeWidth / 2 * Math.cos(angleVarietyradian(angle)) + (mEdgeWidth / 2 + mScaleWidth / 2) * Math.cos(angleVarietyradian(angle)));

        outStopY = (float) Math.abs(mCenterY - (mEdgeRadius + mEdgeLength) * Math.sin(angleVarietyradian(angle)) + mEdgeWidth / 2 * Math.cos(angleVarietyradian(angle)) + (mEdgeWidth / 2 + mScaleWidth / 2) * Math.cos(angleVarietyradian(angle)));


    }

    public static double PI = 3.1415926;
    Map<String, Float> map;

    //角度转弧度方法
    private double angleVarietyradian(int angle) {
        double var = (PI / 180) * angle;
        return var;
    }


    //计算要绘制的点 ，用Map保存 这方法好像行不通
    private void calculationDotSave() {

        int count = 0;//计数第几条线
        for (int i = 0; i <= 180; i += 4) {
            coordinate(i);
            map.put("startX" + count, startX);
            map.put("startY" + count, startY);
            map.put("stopX" + count, stopX);
            map.put("stopY" + count, stopY);
//            Log.d(TAG, "calculationDotSave: .....");
            count++;

        }
    }

    //内刻度 起始点距父控件的距离
    private float padding = DensityUtil.dip2px(getContext(), 21);
    //中心点Y坐标偏移量
    private float offsetY = DensityUtil.dip2px(getContext(), 1);

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        Log.d(TAG, "onMeasure: ........");
        mCenterX = getWidth() / 2;
        mCenterY = getHeight() - offsetY;


        if (mCenterX > mCenterY) {
            mScaleRadius = mCenterY - padding;
            mEdgeRadius = mCenterY - padding + mScaleLength + dis;
        } else {
            mScaleRadius = mCenterX - padding;
            mEdgeRadius = mCenterX - padding + mScaleLength + dis;
        }

        calculationDotSave();
    }


}
